home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MATH.SWG / 0087_Fastest Pi Calculator Yet!.pas < prev    next >
Pascal/Delphi Source File  |  1995-03-03  |  4KB  |  90 lines

  1. {
  2. > Here are my results using a 33MHz 386.
  3. > Pi v2                               Pi v3
  4. > 100  places =  0.1 sec              100  places = 0.3 sec
  5. > 1000 places = 11   sec              1000 places =  32 sec
  6. > 2135 places = 48   sec              2135 places = 150 sec
  7.  
  8.  Not very impressive IMHO. Here's the result of the program following below
  9. (on my 33MHz 486):
  10. Digits(max 150.000):    100  Done! It took   0.00s.
  11. Digits(max 150.000):   1000  Done! It took   0.27s.
  12. Digits(max 150.000):  10000  Done! It took  28.35s.
  13. Digits(max 150.000):  40000  Done! It took 462.25s.
  14.  
  15.    The two units used should come after this message. Uncomment several write-
  16. commands to get a "fully" operational program rather than this benchmark
  17. version. You then also can skip the Timer unit and the two commands from that
  18. unit (TimerOn and TimerOff) to make the program much smaller (no float math
  19. linked into the program).
  20.    I didn't post my 386 version (which is more than 50% faster), since I'm
  21. under the impression that many of you still are struggling with them ol'
  22. fashion 286'es or worse... :)
  23. }
  24.  
  25. program PiCalc;  { The fastest PI calculator you'll ever find... :) }
  26.  
  27. { From bits and pieces picked up mainly from the FidoNet PASCAL echo }
  28. { Collected, optimized, unitized, etc. by Björn Felten @ 2:203:208 }
  29. { Public Domain  --  Nov 1994 }
  30.  
  31. { SWAG Note: The HugeUtil in this unit is in the NUMBERS.SWG Packet
  32.    - Kerry }
  33.  
  34. uses HugeUtil, Timer; { use Crt if you want fast printout on screen }
  35.                       { don't if you want to be able to redirekt o/p }
  36.  
  37. var
  38.     words, number   : longint;
  39.     nin, link, pii, a239    : HugePtr;
  40.  
  41. procedure ArcCoTan(n : integer; var angle : Huge);
  42. var n2, del, remain : integer;
  43.     positive : boolean;
  44.  
  45. begin                               { corresp. integer operations }
  46.   ZeroHuge(angle,words);            { angle := 0 }
  47.   ZeroHuge(nin^,words);             { nin   := 0 }
  48.   ZeroHuge(link^,words);            { link  := 0 }
  49.   angle.dat[angle.len] := 1;        { angle := 1 }
  50.   DivHuge(angle,n,angle,remain);    { angle := angle div n }
  51.   n2 := n*n;                        { n2    := n * n }
  52.   del := 1;                         { del   := 1 }
  53.   positive := true;
  54.   CopyHuge(angle,nin^);             { nin   := angle }
  55.   repeat
  56.     DivHuge(nin^,n2,nin^,remain);   { nin   := nin div n2 }
  57.     inc(del, 2);                    { del   := del + 2 }
  58.     positive := not positive;
  59.     DivHuge(nin^,del,link^,remain); { link  := nin div del }
  60.     if positive then
  61.       AddHuge(angle,link^)          { angle := angle + link }
  62.     else
  63.       SubHuge(angle,link^);         { angle := angle - link }
  64. {    write(#13,word(del)) } { uncomment to see that program is not dead }
  65.   until (link^.len <= 1) and (link^.dat[1] = 0);
  66. {  writeln}                 { ... and this too }
  67. end; { ArcCoTan }
  68.  
  69. begin
  70. {  writeln('Program to get Pi (',pi:1:17,'...) with large precision.'); }
  71.   write('Digits(max 40.000): '); readln(number);
  72.   words := round(number / 4.7) + 3; { appr. 4.7 digits in one word }
  73.   write(number:6,#9);
  74.   TimerOn;
  75.   GetHuge(pii,  words+2);
  76.   GetHuge(a239, words+2);
  77.   GetHuge(link, words+2);
  78.   GetHuge(nin,  words+2);
  79.   ArcCoTan(5,   pii^);        { ATan(1/5)  }
  80.   AddHuge(pii^, pii^);
  81.   AddHuge(pii^, pii^);        { * 4        }
  82.   ArcCoTan(239, a239^);       { ATan(1/239)}
  83.   SubHuge(pii^, a239^);
  84.   AddHuge(pii^, pii^);
  85.   AddHuge(pii^, pii^);        { * 4        }
  86.   TimerOff;
  87. {  WriteHuge(pii^, number)}     { uncomment if you want printout }
  88. end.
  89.  
  90.